<?php
namespace ImiDemo\HttpDemo\MainServer\Controller;

use Imi\Db\Db;
use Imi\Util\Random;
use Imi\Bean\BeanFactory;
use Imi\Util\ObjectArrayHelper;
use Imi\Controller\HttpController;
use Imi\Server\View\Annotation\View;
use Imi\Server\Route\Annotation\Route;
use Imi\Server\Route\Annotation\Action;
use Imi\Server\Route\Annotation\Controller;
use ImiDemo\HttpDemo\MainServer\Model\Team;
use ImiDemo\HttpDemo\MainServer\Model\Test;
use ImiDemo\HttpDemo\MainServer\Model\User;
use ImiDemo\HttpDemo\MainServer\Model\Article;
use ImiDemo\HttpDemo\MainServer\Model\UserSns;
use ImiDemo\HttpDemo\MainServer\Model\UserFriend;
use ImiDemo\HttpDemo\MainServer\Model\UserWithFriend;
use ImiDemo\HttpDemo\MainServer\Model\Taggables;
use ImiDemo\HttpDemo\MainServer\Model\Tags;

/**
 * Model操作 演示
 * @Controller("/ModelPolymorphicM2M/")
 * @View(renderType="json")
 */
class ModelPolymorphicM2MController extends HttpController
{
    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/findArticle
     * 
     * @Action
     * @return void
     */
    public function findArticle()
    {
        $article = Article::find(1);
        return [
            'article'   =>    $article,
        ];
    }

    // /**
    //  * Model操作 演示
    //  * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/findTeam
    //  * 
    //  * @Action
    //  * @return void
    //  */
    // public function findTeam()
    // {
    //     $team = Team::find(1);
    //     $team->avatar->queryRelations('relationModel');
    //     return [
    //         'team'            =>    $team,
    //         'avatarClass'    =>    BeanFactory::getObjectClass($team->avatar->relationModel),
    //     ];
    // }

    // /**
    //  * Model操作 演示
    //  * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/findUser2
    //  * 
    //  * @Action
    //  * @return void
    //  */
    // public function findUser2()
    // {
    //     $user = User::find(1);
    //     $user->avatar->queryRelations('user');
    //     return [
    //         'user'            =>    $user,
    //     ];
    // }

    // /**
    //  * Model操作 演示
    //  * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/select
    //  * 
    //  * @Action
    //  * @return void
    //  */
    // public function select()
    // {
    //     $list = User::select();

    //     return $list;
    // }

    // /**
    //  * Model操作 演示
    //  * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/newInstance
    //  * 
    //  * @Action
    //  * @return void
    //  */
    // public function newInstance()
    // {
    //     $user = User::newInstance();

    //     return $user;
    // }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/insert
     * 
     * @Action
     * @return void
     */
    public function insert()
    {
        $article = Article::newInstance();
        $article->title = '123';
        $article->content = '456';
        $article->taggables->append(
            Taggables::newInstance(['tag_id'=>1]),
            Taggables::newInstance(['tag_id'=>2])
        );
        $result = $article->insert();

        return [
            'success'    =>    $result->getLastInsertId() > 0,
            'data'        =>    $article,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/update
     * 
     * @Action
     * @return void
     */
    public function update()
    {
        $article = Article::find(1);
        
        // $article->taggables->clear();

        $article->taggables->append(
            Taggables::newInstance(['tag_id'=>3])
        );

        $result = $article->update();

        return [
            'success'    =>    $result->isSuccess(),
            'data'        =>    $article,
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/delete
     * 
     * @Action
     * @return void
     */
    public function delete()
    {
        $maxId = Article::query()->max('id');
        $article = Article::find($maxId);
        $result = $article->delete();

        return [
            'success'    =>    $result->isSuccess(),
        ];
    }

    /**
     * Model操作 演示
     * 访问：http://127.0.0.1:8080/ModelPolymorphicM2M/toMany
     * 
     * @Action
     * @return void
     */
    public function toMany()
    {
        $tag = Tags::find(1);
        $tag->queryRelations('articles', 'books');
        return [
            'tag'       =>  $tag,
        ];
    }
}